/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.abokatua;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Abokatua;

@SuppressWarnings("serial")
public class AbokatuBaja extends Panela {
	private JButton ok, ezeztatu;
	private JLabel zenbakia;
	private JTextField zenb;
	private Abokatua abok=new Abokatua();

	public AbokatuBaja(String tituloa, final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(5, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Epaile NAN:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		zenb = new JTextField(15);
		zenb.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (zenb.getText().length() >= 10) {
					arg0.consume();
				}
			}
		});
		arri.add(zenbakia);
		arri.add(zenb);
		erdikoa.add(arri);

		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout arrib = new FlowLayout();
		arrib.setHgap(15);
		arrib.setVgap(10);
		arribb.setLayout(arrib);
		erdikoa.add(arribb);

		JPanel c = new JPanel();
		c.setBackground(SystemColor.activeCaption);
		erdikoa.add(c);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Baja eman");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunean\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!abok.existitzenDa(zenb.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da abokatu hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						abok.aboEzabatu((zenb.getText()));
						JOptionPane.showMessageDialog(null,
								"Abokatua kenduta!", "",
								JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});

}
	
	

	@Override
	public void eremuakGarbitu() {
		zenb.setText("");
		
	}
	private boolean zerbaitIdatzi(){
		boolean aurki;
		aurki= zenb.getText().length() > 0;
   		return aurki;
   	}
}
